package com.pcxm.redis.test;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;

public abstract class AbstractRedisService<K, V> {  
    
    @Autowired  
     private RedisTemplate<K, V> redisTemplate;  
    
     public RedisTemplate<K, V> getRedisTemplate() {  
         return redisTemplate;  
     }  
    
     public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {  
         this.redisTemplate = redisTemplate;  
     }  
     
     public void set(final K key, final V value, final long expiredTime) {  
         BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);  
         if (expiredTime <= 0) {  
             valueOper.set(value);  
         } else {  
             valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);  
         }  
     }  
    
     public V get(final K key) {  
         BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);  
         return valueOper.get();  
     } 
     
     public Object getHash(K key, String name){
         Object res = redisTemplate.boundHashOps(key).get(name);
         return res;
     }
    
     public void del(K key) {  
         if (redisTemplate.hasKey(key)) {  
             redisTemplate.delete(key);  
         }  
     }   
    
 } 